package com.zerui.manager.accessauthorization.domainservice;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zerui.basedata.client.PersonnelService;
import com.zerui.cola.dto.Response;
import com.zerui.cola.dto.SingleResponse;
import com.zerui.manager.access.auth.task.domain.AuthTask;
import com.zerui.manager.access.auth.task.gateway.AuthTaskGateway;
import com.zerui.manager.accessauthdevice.gateway.AccessAuthDeviceGateWay;
import com.zerui.manager.accessauthorg.gateway.AccessAuthOrgGateWay;
import com.zerui.manager.accessauthorization.AccessAuth;
import com.zerui.manager.accessauthorization.gateway.AccessAuthGateWay;
import com.zerui.manager.accessauthpersonnel.gateway.AccessAuthPersonnelGateWay;
import com.zerui.manager.web.cmd.AccessAuthQryCmd;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * @Author wh
 * @create_time 2023-08-23 11:07
 */
@Component
public class AccessAuthRepository {

	@Resource
	private AccessAuthGateWay accessAuthGateWay;
	@Resource
	private AccessAuthDeviceGateWay accessAuthDeviceGateWay;
	@Resource
	private AccessAuthOrgGateWay accessAuthOrgGateWay;
	@Resource
	private AccessAuthPersonnelGateWay accessAuthPersonnelGateWay;
	@Resource
	private PersonnelService personnelService;
	@Resource
	private AuthTaskGateway authTaskGateway;

	public Response add(AccessAuth accessAuth) {
		return accessAuthGateWay.add(accessAuth);
	}

	public Response update(AccessAuth accessAuth) {
		return accessAuthGateWay.update(accessAuth);
	}

	public AccessAuth selectById(Long id) {
		return accessAuthGateWay.selectById(id);
	}

	public Response deleteById(Long id) {
		return accessAuthGateWay.deleteById(id);
	}

	public Page<AccessAuth> getPageList(AccessAuthQryCmd accessAuthQryCmd) {
		return accessAuthGateWay.getPageList(accessAuthQryCmd);
	}

	public void distributeAccessAuth(Long accessAuthId) {
		List<Long> deviceIds = accessAuthDeviceGateWay.getDeviceIdsByAccessAuthId(accessAuthId);
		List<Long> orgIds = accessAuthOrgGateWay.getAccessAuthOrgById(accessAuthId);
		List<Long> personals = accessAuthPersonnelGateWay.getAccessAuthPersonnelById(accessAuthId);
		Set<Long> personnelList = getMergePersonnel(orgIds,personals);
		if(CollectionUtils.isEmpty(personnelList)){
			return;
		}
		personnelList = accessAuthPersonnelGateWay.filterAccessAuthPersonnelByIds(accessAuthId,personnelList);

		if(CollectionUtils.isEmpty(personnelList)){
			return;
		}
		authTaskGateway.saveAuthTask(AuthTask.ofUnbindOrg(accessAuthId, deviceIds, personnelList));
//		CompletableFuture.runAsync(()-> accessAuthGateWay.addDistribute(deviceIds, finalPersonnelList,accessAuthId));

	}

	private Set<Long> getMergePersonnel(List<Long> orgIds,List<Long> personals) {

		List<Long> perLists = new ArrayList<>(personals);
		//查询orgIds部门下的所有人员
		SingleResponse<Map<Long, List<Long>>> result = personnelService.getPersonnelIdsByOrgIds(orgIds);

		Map<Long, List<Long>> map = result.getData();

		if(CollectionUtils.isNotEmpty(map)){
			perLists.addAll(
					map.values()
							.stream()
							.flatMap(List::stream)
							.collect(Collectors.toList())
			);
		}

		return new HashSet<>(perLists);
	}

	public List<AccessAuth> selectAll() {
		return accessAuthGateWay.selectAll();
	}
}
